//
//  FindViewController.m
//  Beauty
//
//  Created by Snow on 16/8/29.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "FindViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "GXCycleView.h"
#import "QualityLifeTableViewCell.h"
#import "HotWindowTableViewCell.h"
#import "HotTopicTableViewCell.h"
#import "RelatedProductsTableViewCell.h"
#import "ModelOfCycleFind.h"
#import "DetailQualityViewController.h"
#import "NewProductViewController.h"
#import "HotWindowViewController.h"
#import "LookMoreWindowViewController.h"
#import "HotTopicViewController.h"
#import "LookMoreTopicViewController.h"
#import "FeaturedViewController.h"
@interface FindViewController ()<UITableViewDelegate, UITableViewDataSource, GXCycleDelegate, QualityDelegate, RelateProductDelegate, HotWindowDelegate, HotTopicDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfCycleFind;
@property (nonatomic, strong) NSMutableArray *arrOfSpecialID;
@end

@implementation FindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
    label.text = @"发现";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    label.textAlignment = 1;
    self.navigationItem.titleView = view;
    [self createView];
    [self createFresh];
}
- (void)createFresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    _arrayOfCycleFind = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=index&op=special_list_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = responseObject[@"datas"];
        NSArray *array = [dictionary objectForKey:@"banner"];
        for (NSDictionary *dic in array) {
            ModelOfCycleFind *model = [[ModelOfCycleFind alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfCycleFind addObject:model.special_banner];
        }
        GXCycleView *header = [[GXCycleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
        header.arrayImage = _arrayOfCycleFind;
        _tableView.tableHeaderView = header;
        header.delegate = self;
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];

    [self.tableView.mj_header endRefreshing];
}

- (void)handleData
{
    _arrOfSpecialID = [NSMutableArray array];
    _arrayOfCycleFind = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=index&op=special_list_1_5" par:nil success:^(id responseObject) {
    NSDictionary *dictionary = responseObject[@"datas"];
    NSArray *array = [dictionary objectForKey:@"banner"];
    for (NSDictionary *dic in array) {
        ModelOfCycleFind *model = [[ModelOfCycleFind alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_arrayOfCycleFind addObject:model.special_banner];
        [_arrOfSpecialID addObject:model];
    }
    GXCycleView *header = [[GXCycleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    header.arrayImage = _arrayOfCycleFind;
    _tableView.tableHeaderView = header;
        header.delegate = self;
    [_tableView reloadData];
} filed:^(NSError *error) {
    
}];
}
- (void)createView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[QualityLifeTableViewCell class] forCellReuseIdentifier:@"QualityLifeTableViewCell"];
    [_tableView registerClass:[HotWindowTableViewCell class] forCellReuseIdentifier:@"HotWindowTableViewCell"];
    [_tableView registerClass:[HotTopicTableViewCell class] forCellReuseIdentifier:@"HotTopicTableViewCell"];
    [_tableView registerClass:[RelatedProductsTableViewCell class] forCellReuseIdentifier:@"related"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (__kindof UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        QualityLifeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QualityLifeTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 1) {
        HotWindowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotWindowTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 2) {
        HotTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotTopicTableViewCell"];
        cell.delegate = self;
        return cell;
    } else {
        RelatedProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"related"];
        cell.delegate = self;
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        return 100;
    } else {
    return 200;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = @"品质生活";
        [view addSubview:label];
        return view;
    } else if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = @"热门橱窗";
        [view addSubview:label];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 50);
        [button setTitle:@"查看更多 >" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tintColor = [UIColor lightGrayColor];
        [view addSubview:button];
        return view;
    } else if(section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = @"热门话题";
        [view addSubview:label];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(SCREEN_WIDTH - 100, 0, 100, 50);
        [button setTitle:@"查看更多 >" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(handleButtonOfTopic:) forControlEvents:UIControlEventTouchUpInside];
        button.tintColor = [UIColor lightGrayColor];

        [view addSubview:button];
        return view;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
        label.text = @"猜你喜欢";
        [view addSubview:label];
        return view;
    }
}

//轮播图点击方法
- (void)backOneNsstring:(NSIndexPath *)indexPath
{
    ModelOfCycleFind *model = [_arrOfSpecialID objectAtIndex:indexPath.row];
    FeaturedViewController *vc = [[FeaturedViewController alloc]init];
    vc.special_id = model.special_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
//品质生活点击方法
- (void)quality
{
    DetailQualityViewController *quality = [[DetailQualityViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:quality animated:YES];
}
//cai ni xi huan
- (void)relateWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = good_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];

}
//re men chu chuang
- (void)hotWindow:(NSString *)favorites_class_id
{
    HotWindowViewController *hot = [[HotWindowViewController alloc] init];
    hot.favorita_class_id = favorites_class_id;
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:hot animated:YES];
}
//热门话题
- (void)hotTopicWithID:(NSString *)class_id
{
    HotTopicViewController *hot = [[HotTopicViewController alloc] init];
    hot.class_id = class_id;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:hot animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (void)handleButton:(UIButton *)button
{
    LookMoreWindowViewController *look = [[LookMoreWindowViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:look animated:YES];
}
- (void)handleButtonOfTopic:(UIButton *)button
{
    LookMoreTopicViewController *look = [[LookMoreTopicViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController pushViewController:look animated:YES];
}
#pragma mark ------------  去掉tableView的头部分区悬浮效果  -----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

@end
